<?php $this->load->view('header');?>

<body class="childrenBody">
    <blockquote class="layui-elem-quote news_search">
		<form class="layui-form" action="" style="display: inline;">
			<div class="layui-inline">

				<div class="layui-input-inline">
			    	<select name="type_id" id="type_id">
					  <option value="" selected>全部类型</option>
					  <?php foreach ($type_list as $item):?>
					  <option value="<?=$item['id']?>"><?=$item['name']?></option>
					  <?php endforeach;?>
					</select> 
			    </div>

			    <div class="layui-input-inline">
			    	<input type="text" name="keyword" value="" placeholder="搜标题" class="layui-input search_input">
			    </div>
			    <a class="layui-btn layui-btn-normal Sear_btn"><i class="layui-icon">&#xe615;</i>查询</a>
			</div>
		</form>
		<div style="float: right;">
			<div class="layui-inline">
				<a class="layui-btn layui-btn-primary Cat_btn"><i class="layui-icon">&#xe654;</i> 文章类型</a>
			</div>
			<div class="layui-inline">
				<a class="layui-btn layui-btn-custom userAdd_btn"><i class="layui-icon">&#xe654;</i> 文章</a>
			</div>
		</div>
	</blockquote>

	<!-- 视图 -->
	<div class="layui-form news_list">
		<form class="layui-form">
			<table class="layui-table" lay-even="" lay-skin="line">
			    <thead>
				<tr>
					<th>标题</th>
					<th>文章类型</th>
					<th>点赞（次）</th>
					<th>收藏（次）</th>
					<th>分享（次）</th>
					<th>操作</th>
				</tr> 
			    </thead>
				<tbody class="goods_content" id="view"></tbody>
				
			</table>
			<div class="page_box" style="float: right;padding-right: 10px;">
				<div id="listpage"></div>
			</div>
		</form>
	</div>

	<!-- 模板 -->
	<script id="demo" type="text/html">
	    {{#  layui.each(d.list, function(index, item){ }}
			<tr>
			  <td>{{ item.title }}</td>
			  <td>{{ item.type_name }}</td>
			  <td>{{ item.likes }}</td>
			  <td>{{ item.collection }}</td>
			  <td>{{ item.share }}</td>
			  <td>
			    <a class="layui-btn layui-btn-warm layui-btn-mini" id="edit{{ item.id }}" title="编辑">
			      <i class="layui-icon">&#xe642;</i>编辑
			    </a>
			    <a class="layui-btn layui-btn-danger layui-btn-mini" id="delete{{ item.id }}" title="删除">
			      <i class="layui-icon">&#xe640;</i>删除
			    </a>
			  </td>
			</tr>
	    {{#  }); }}

	    {{#  if(d.list.length === 0){ }}
	      暂无数据
	    {{#  } }} 
	</script>

	<script type="text/javascript">
	layui.config({
		version: true
	}).use(['jquery','layer', 'laytpl', 'form', 'element', 'laypage'],function(){
		var layer = parent.layer === undefined ? layui.layer : parent.layer,
			laytpl = layui.laytpl,
			form = layui.form,
			element = layui.element,
			laypage = layui.laypage,
			$ = layui.jquery;

			load_list();

			//查询
			$('.Sear_btn').click(function() {
				var keyword = $("input[name='keyword']").val();
				var type_id = $('#type_id').val();
				load_list('',keyword,type_id);
			});

			//添加
			$('.userAdd_btn').click(function(event) 
			{
		        var index = layui.layer.open({
					title : "添加文章",
					type : 2,
					content : "admin.php/Article/add_article",
					cancel: function(index, layero){ 
							location.reload();						 
					}  
				})

				//改变窗口大小时，重置弹窗的高度，防止超出可视区域
				$(window).resize(function(){
					layui.layer.full(index);
				})
				layui.layer.full(index);
			});


			//设置分类
			$('.Cat_btn').click(function() 
			{

				var index = layui.layer.open({
					title : "设置文章类型",
					type : 2,
					content : "admin.php/Article/type_list",
					cancel: function(index, layero){ 
							location.reload();						 
					}  
				})

				//改变窗口大小时，重置弹窗的高度，防止超出可视区域
				$(window).resize(function(){
					layui.layer.full(index);
				})
				layui.layer.full(index);
			});



			function load_list(p,k,t)
			{
				$.get('admin.php/Article/get_list',{page:p,keyword:k,type_id:t}, function(data) 
				{
					var getTpl = demo.innerHTML;
					laytpl(getTpl).render(data, function(html){
					  $('#view').html(html);
					});


					layui.each(data.list, function(index, el) 
					{
						//分页
						laypage.render({
						  elem: 'listpage'
						  ,count: data.count //数据总数，从服务端得到
						  ,theme: '#1E9FFF'
						  ,limit: 50
						  ,curr:p
						  ,jump: function(obj, first){
						    //首次不执行
						    if(!first){
						      load_list(obj.curr,k,t);
						    }
						  }
						});

						//编辑
						$('#edit'+el.id).click(function() 
						{
							var index = layui.layer.open({
								title : "编辑文章",
								type : 2,
								content : "admin.php/Article/add_article/"+el.id,
								cancel: function(index, layero){ 
									location.reload();						 
								}  
							})

							//改变窗口大小时，重置弹窗的高度，防止超出可视区域
							$(window).resize(function(){
								layui.layer.full(index);
							})
							layui.layer.full(index);
						});

						//删除
						$('#delete'+el.id).click(function() 
						{
							layer.confirm('确定删除选中的信息？',{icon:3, title:'提示信息'},function(index){
								$.get('admin.php/Article/delete_article',{id:el.id}, function(res) {
									if (res.status == 200) {
										layer.msg(res.message, {icon:1});
										location.reload();
									}
									else
									{
										layer.msg(res.message, {icon:2});
									}
								},"json");
							});
						});

						form.render(); //更新全部
					});
				},'json');

			}

	});
	</script>

</body>
</html>